﻿using System;
using Newtonsoft.Json;
#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif
using UnityEngine;

namespace JH
{
    public partial class AroundWithMoveCameraConfig
    {
        [Serializable]
        public record ZoomConfig
        {
            /// <summary>
            ///  是否允许缩放
            /// </summary>
#if ODIN_INSPECTOR
            [LabelText("是否允许缩放")]
#endif
            public bool allowZoom;

            /// <summary>
            ///  缩放阻尼
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowZoom")] [LabelText("缩放速度")]
#endif
            public float zoomSpeed = 3;

            /// <summary>
            ///  归一化半径-缩放速度的变化曲线
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowZoom")]
            [LabelText("归一化半径-缩放速度的变化曲线")]
            [SerializeField]
#endif
            [JsonIgnore]
            private AnimationCurve zoomSpeedCurve = AnimationCurve.Linear(0, 1, 1, 1);

            /// <summary>
            ///  缩放阻尼
            ///  </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowZoom")] [LabelText("缩放阻尼")]
#endif
            public float zoomDamping;

            /// <summary>
            ///  缩放范围
            /// </summary>
#if ODIN_INSPECTOR
            [ShowIf("allowZoom")] [LabelText("缩放范围")]
#endif
            public Vector2 zoomRange = new Vector2(0.1f, 100f);

            /// <summary>
            ///  获取归一化的缩放半径
            /// </summary>
            public float GetNormalizedRadius(float radius)
            {
                // 计算距离百分比
                var clamp = ClampZoom(radius);
                var currentDistance = Mathf.InverseLerp(zoomRange.x, zoomRange.y, clamp);

                // 返回归一化的半径
                return currentDistance;
            }

            /// <summary>
            ///  限制缩放范围
            /// </summary>
            public float ClampZoom(float radius)
            {
                // 限制缩放范围
                return Mathf.Clamp(radius, zoomRange.x, zoomRange.y);
            }

            /// <summary>
            ///  获取缩放速度
            /// </summary>
            public float GetZoomSpeed(float radius)
            {
                // 如果没有曲线，直接返回1
                if (zoomSpeedCurve == null) return zoomSpeed;

                // 获取曲线在t处的值
                return zoomSpeedCurve.Evaluate(GetNormalizedRadius(radius)) * zoomSpeed;
            }
        }
    }
}